package com.controller;

import com.comment.CommentResult;
import com.domain.entity.Accessory;
import com.domain.req.AddAccessoryReq;
import com.domain.req.PageReq;
import com.domain.req.UpdateAccessoryReq;
import com.domain.vo.AccessoryPageListVO;
import com.github.pagehelper.PageInfo;
import com.service.AccessoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 配件相关业务
 */
@CrossOrigin//跨越
@RestController
@Slf4j
@RequestMapping("/accessory")
public class AccessoryController {
    @Autowired
    private AccessoryService accessoryService;

    /**
     * 新增配件信息
     */
    @PostMapping("/addAccessory")
    public CommentResult<Void> addAccessory(@RequestBody AddAccessoryReq addAccessoryReq) {
        accessoryService.addAccessory(addAccessoryReq);
        return CommentResult.success();
    }

    /**
     * 根据id删除配件信息
     */
    @DeleteMapping("/deleteById/{id}")
    public CommentResult<Void> deleteById(@PathVariable("id") Integer id) {
        accessoryService.deleteById(id);
        return CommentResult.success();
    }

    @PutMapping("/updateById")
    public CommentResult<Void> updateById(@RequestBody  UpdateAccessoryReq updateAccessoryReq) {
        accessoryService.updateById(updateAccessoryReq);
        return CommentResult.success();
    }


    /**
     *  根据商品id分页查询商品列表
     */
    @GetMapping("/listPageByGoodsId/{goodsId}")
    public CommentResult<List<AccessoryPageListVO>> listPageByGoodsId(@PathVariable("goodsId") Integer goodsId) {
        List<AccessoryPageListVO> list = accessoryService.listByGoodsId(goodsId);
        return CommentResult.success(list);
    }

    /**
     * 分页查询配件信息
     */
    @GetMapping("/pageList")
    public CommentResult<PageInfo<AccessoryPageListVO>> pageList(PageReq pageReq, Accessory accessory) {
        PageInfo<AccessoryPageListVO> pageInfo = accessoryService.pageList(pageReq, accessory);
        return CommentResult.success(pageInfo);
    }

    @GetMapping("/getById/{id}")
    public CommentResult<Accessory> getById(@PathVariable("id") Integer id){
        Accessory accessory = accessoryService.getById(id);
        return CommentResult.success(accessory);
    }

    @GetMapping("/searchByName")
    public CommentResult<List<Accessory>> searchByName(String name) {
        List<Accessory> list = accessoryService.searchByName(name);
        return CommentResult.success(list);
    }



}
